package com.itheima.health.service.impl;

import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //判断当前日期是否已经进行了预约设置
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //已经进行了预约设置，进行修改操作
                    orderSettingMapper.editNumberByOrderDate(orderSetting);
                } else {
                    //没有进行预约设置，进行添加操作
                    orderSettingMapper.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String month) {
        //解析输入的月份字符串，得到开始日期和结束日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        YearMonth yearMonth = YearMonth.parse(month, formatter);

        //获取该月份的第一天和最后一天
        LocalDate firstDayOfMonth = yearMonth.atDay(1);
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();

        //格式化日期为标准格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateBegin = firstDayOfMonth.format(dateTimeFormatter);
        String dateEnd = lastDayOfMonth.format(dateTimeFormatter);

        //转换为java.sql.Date类型
        java.sql.Date sqlDateBegin = java.sql.Date.valueOf(firstDayOfMonth);
        java.sql.Date sqlDateEnd = java.sql.Date.valueOf(lastDayOfMonth);

        //创建映射参数
        Map<String, java.sql.Date> map = new HashMap<>();
        map.put("dateBegin", sqlDateBegin);
        map.put("dateEnd", sqlDateEnd);
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map oderSettingMap = new HashMap();
            oderSettingMap.put("date", orderSetting.getOrderDate().getDate());//日期
            oderSettingMap.put("number", orderSetting.getNumber());//可预约人数
            oderSettingMap.put("reservations", orderSetting.getReservations());//已预约人数
            data.add(oderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //已经进行了预约设置，进行修改操作
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        } else {
            //没有进行预约设置，进行添加操作
            orderSettingMapper.add(orderSetting);
        }
    }
}
